package br.com.vti.sisp_sicrim.modelo.business;

import javax.validation.ValidationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import br.com.crud4j.core.model.annotation.Constraints;
import br.com.crud4j.core.model.business.BusinessImpl;
import br.com.crud4j.core.model.dao.DAO;
import br.com.vti.sisp_sicrim.model.entity.PerfilUsuario;
import br.com.vti.sisp_sicrim.model.entity.Usuario;
import br.com.vti.sisp_sicrim.modelo.business.exception.PerfilUsuarioException;

/**
 *
 * @author ednardomatos
 */
@Component("perfilUsuarioBusiness")
@Transactional("sispSicrimTransactionManager")
public class PerfilUsuarioBusinessImpl extends BusinessImpl<PerfilUsuario, PerfilUsuarioException> {
	private final Integer ADMIN = 1;

	@Autowired
	public PerfilUsuarioBusinessImpl(@Qualifier("sispSicrimDao") DAO<PerfilUsuario> dao) {
		super(dao);
	}

	@Override
	protected void validate(Integer operacao, PerfilUsuario perfilUsuario) throws ValidationException {
		if (operacao == BusinessImpl.INSERIR || operacao == BusinessImpl.ATUALIZAR || operacao == BusinessImpl.REMOVER) {
			if (perfilUsuario != null && perfilUsuario.getId() != null && perfilUsuario.getId().intValue() == ADMIN) {
				throw new ValidationException(getMessageResolver().getMessage("erro.usuario.admin"));
			}
		}
		super.validate(operacao, perfilUsuario);
	}

}
